package com.javadroid.view;

import net.simonvt.menudrawer.MenuDrawer;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;

import com.javadroid.dao.FileProcess;
import com.javadroid.facade.SmartNovelasFacade;
import com.javadroid.fragments.FlipFragment;
import com.javadroid.fragments.FragmentEmissoras;
import com.javadroid.fragments.FragmentNovelas;
import com.javadroid.service.Trigger;
import com.javadroid.smartnovelas.R;


public class SlideFragment extends Activity implements FragmentEmissoras.EmissorasListener,FragmentNovelas.NovelasListener{

    private CharSequence mTitle;
    public int index_fragment = 0;
	
    private int emissora;
	private SmartNovelasFacade artigo;
	private String[] array_novelas;
	public static double ROW_EMISSORA_TAMANHO;
	public static double ROW_EMISSORA_LARGURA;
	

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.main);
	artigo = new SmartNovelasFacade(getApplicationContext());
	
	new Trigger(this);
	
    	if (savedInstanceState == null) {
    		Fragment fragment = new FragmentEmissoras();
    		FragmentTransaction ft = getFragmentManager().beginTransaction();
    		ft.add(R.id.content_frame, fragment).commit();
    	}

   	FileProcess f = new FileProcess(getApplicationContext());
   	f.copyAssets();
   	
	getTamanhoDaTela();
   	
  }
	private void getTamanhoDaTela() {
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		int height = size.y;
		int width = size.x;
		
		ROW_EMISSORA_LARGURA = (width / 2);
		double valor_processado = (height / 3);
		double valor_percent = 20.0 / 100.0;
		ROW_EMISSORA_TAMANHO = valor_processado - (valor_percent * valor_processado);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.update_menu_item, menu);
	    setProgressBarIndeterminateVisibility(false);
	    menu.findItem(R.id.box_novelas).setVisible(false);
	    menu.findItem(R.id.menu_item_update).setVisible(false);
	    return super.onCreateOptionsMenu(menu);
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    return super.onOptionsItemSelected(item);			
	}

    public void buscaIndiceCorreto(int position) {
    	String a = array_novelas[position];
		artigo.getIdNovela(a);
	}

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

	@Override
	public void emissoraSelected(int position) {
		index_fragment = 1;
		this.emissora = position;
		invalidateOptionsMenu();
	}

	@Override
	public void novelaSelected(int positionReal) {
    	getCapitulosFlipFragmentThread(positionReal);
	}
	
	@Override
	protected void onDestroy() {
		//ImageLoader.getInstance().clearDiscCache();
		super.onDestroy();
	}	
	
	public void getCapitulosFlipFragmentThread(final int id_novela) {

		index_fragment = 2;
		Fragment fragment = new FlipFragment();
		Bundle args = new Bundle();
		args.putInt(FlipFragment.ARG_NOVELA, id_novela);
		fragment.setArguments(args);

		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.setCustomAnimations(
				R.animator.fragment_slide_left_enter,
				R.animator.fragment_slide_left_exit);
		ft.replace(R.id.content_frame, fragment);
		ft.commit();

	}
			
	public void onBackPressed() {
		setProgressBarIndeterminateVisibility(false);
		
		try {
			final int drawerState = FlipFragment.mMenuDrawer.getDrawerState();
			if (drawerState == MenuDrawer.STATE_OPEN || drawerState == MenuDrawer.STATE_OPENING) {
				FlipFragment.mMenuDrawer.closeMenu();
				return;
			}
		} catch (NullPointerException e) {

		}
		

		if(index_fragment == 0){
			super.onBackPressed();
		}
		if(index_fragment == 1){
			Fragment fragment =  new FragmentEmissoras();
	        FragmentTransaction ft = getFragmentManager().beginTransaction();
	        ft.setCustomAnimations(
	        		R.animator.fragment_slide_right_enter,
	        		R.animator.fragment_slide_right_exit);
			
	        ft.replace(R.id.content_frame, fragment);
	        ft.commit();
			
	        setTitle("Emissoras");
			index_fragment--;
         
		}
		if(index_fragment == 2){
			
			Fragment fragment = new FragmentNovelas();
	        
	        FragmentTransaction ft = getFragmentManager().beginTransaction();
	        Bundle args = new Bundle();
			args.putInt(FragmentNovelas.ARG_EMISSORA, emissora);
	        fragment.setArguments(args);
	        ft.setCustomAnimations(
	        		R.animator.fragment_slide_right_enter,
	        		R.animator.fragment_slide_right_exit);
			
	        ft.replace(R.id.content_frame, fragment);
	        ft.commit();
			
	        setTitle("Novelas");
			index_fragment--;

		}
		invalidateOptionsMenu();
	}
	
	public boolean isOnline() {
	    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    return cm.getActiveNetworkInfo() != null &&  cm.getActiveNetworkInfo().isConnectedOrConnecting();
	}
	
}
  
  

